package com.wgts.wgts.mvp.view.activity.mycenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerMyCenterNikeNameActivityComponet;
import com.wgts.wgts.dagger.module.MyCenterNikeNameModule;
import com.wgts.wgts.mvp.presenter.IMyCenterNikeNameActivityPresenter;
import com.wgts.wgts.mvp.view.IMyCenterNikeNameActivityView;
import com.wgts.wgts.netmodle.resp.EditMyCenterMessageEntity;
import com.wgts.wgts.tools.AppInfor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/9/2811:38
 *
 */public class MyCenterNikeNameActivity extends BaseActivity implements IMyCenterNikeNameActivityView{
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.change_nike_name_et)
    EditText changeNikeNameEt;
    @BindView(R.id.nike_name_length)
    TextView nikeNameLength;
    @BindView(R.id.change_nike_name_ly)
    RelativeLayout changeNikeNameLy;

    @Inject
    IMyCenterNikeNameActivityPresenter presenter;

    private int REQUEST_CHANGE_NIKE_NAME = 2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_center_nike_name_activity);
        ButterKnife.bind(this);
        includeTitle.setText("编辑昵称");
        Intent intent = getIntent();
        changeNikeNameEt.setHint(intent.getStringExtra("hint"));
        changeNikeNameLy.setClickable(false);
        intView();
    }

    @Override
    public void init() {
        DaggerMyCenterNikeNameActivityComponet
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .myCenterNikeNameModule(new MyCenterNikeNameModule(this))
                .build()
                .inject(this);
    }
    private void intView(){
        editChange();
    }

    @OnClick({R.id.back_btn_ly, R.id.change_nike_name_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.change_nike_name_ly:
                showDialog();
                presenter.changeNikeName("UPDATE_USER_NICKNAME", changeNikeNameEt.getText().toString());
                break;
        }
    }

    private void editChange(){
        final InputFilter[] filters = {new NameLengthFilter(24)};
        changeNikeNameEt.setFilters(filters);
        changeNikeNameEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                checkEditText(s.toString());
                if (s.length() > 0){
                    changeNikeNameLy.setBackgroundResource(R.drawable.price_order);
                    changeNikeNameLy.setClickable(true);
                }else {
                    changeNikeNameLy.setBackgroundResource(R.drawable.nike_name_border);
                    changeNikeNameLy.setClickable(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    private void checkEditText(String txt){
        int chartDest = 0;
        for (int i = 0; i< txt.length(); i++){
            String subStr = txt.substring(i, i+1);
            Pattern p = Pattern.compile("[0-9]");
            Matcher m = p.matcher(subStr);
            if(m.find() ){
                chartDest++;
            }else {
                p=Pattern.compile("[a-zA-Z]");
                m=p.matcher(subStr);
                if(m.find()){
                    chartDest++;
                }else {
                    chartDest = chartDest+2;
                }
            }
        }
        nikeNameLength.setText(chartDest + "/24");
    }

    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        MoHuToast.showToast(msg);
    }

    @Override
    public void changeNikeName(EditMyCenterMessageEntity entity) {
        dialogDismiss();
        MoHuToast.showToast("修改成功");
        AppInfor.getInstance().setUserName(entity.userNickName);
       Intent intent = new Intent();
       intent.putExtra("result", 1);
       setResult(REQUEST_CHANGE_NIKE_NAME, intent);
       finish();
    }
}
